package com.grade.controller;

import com.grade.Beans.Scores;
import com.grade.Beans.Subjects;
import com.grade.service.ScoresService;
import com.grade.service.StudentService;
import com.grade.utils.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/scores")
@ResponseBody
public class ScoresController {
    @Autowired
    ScoresService scoresService;
    @RequestMapping("/getAllScores")
    public Results<List<Scores>> findAllScores(@RequestParam(value = "stuName", defaultValue = "") String stuName,
                                                 @RequestParam(value = "page", defaultValue = "1") int page,
                                                 @RequestParam(value = "limit", defaultValue = "5") int pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("stuName", "%" + stuName + "%");
        map.put("startpage", (page - 1) * pageSize);
        map.put("pageSize", pageSize);
        List<Scores> scoresList = scoresService.findByScoresPage(map);
        long count = scoresService.findByScores(map);
        Results<List<Scores>> results = new Results<>("0", "查询成功", scoresList, count);
        return results;
    }


    @RequestMapping("/addScore")
    public Results<Scores> addScores(Scores scores) {
        Results<Scores> results = null;
            int count = scoresService.addScores(scores);
            if (count == 1) {
                results = new Results<>("0", "新增成功");
            } else {
                results = new Results<>("2", "新增失败");
            }
        return results;
    }

    @RequestMapping("/deleteScores")
    public Results<Scores> deleteScores(int id) {
        int count = scoresService.deleteScores(id);
        Results<Scores> results = null;
        if (count == 1) {
            results = new Results<>("0", "删除成功");
        } else {
            results = new Results<>("1", "删除失败");
        }
        return results;
    }

    @RequestMapping("/updateScores")
    public Results<Scores> updateScores(Scores scores) {
        System.out.println(scores.toString());
        int count = scoresService.updateScores(scores);
        Results<Scores> results = null;
        if (count == 1) {
            results = new Results<>("0", "更新成功");
        } else {
            results = new Results<>("2", "更新失败");
        }
        return results;
    }
    @RequestMapping("/getMyScores")
    public Results getMyScores(HttpSession session){
        int studentNo=(Integer)session.getAttribute("id");
        List<Scores> scoreslist=scoresService.findByScoresStudent(studentNo);
        return new Results("0","查询成功",scoreslist,null);
    }
}
